package mo.tags.stack;

import java.util.Stack;

public class L227 {

    public int calculate(String s) {
        Stack<Integer> numbers = new Stack<>();
        int cur = 0;
        char pre = '+';
        for (int i = 0; i < s.length(); i++) {
            if (Character.isDigit(s.charAt(i))) {
                cur = cur * 10 + (s.charAt(i) - '0');
            } else if (s.charAt(i) != ' ') {
                if (pre == '+') {
                    numbers.push(cur);
                } else if (pre == '-') {
                    numbers.push(-cur);
                } else if (pre == '*') {
                    numbers.push(numbers.pop()*cur);
                } else if (pre == '/') {
                    numbers.push(numbers.pop() / cur);
                }
                cur = 0;
                pre = s.charAt(i);
            }
            if (i == s.length() - 1 && cur != 0) {
                if (pre == '*') {
                    numbers.push(numbers.pop()*cur);
                }
                if (pre == '/') {
                    numbers.push(numbers.pop() / cur);
                }
                if (pre == '+') {
                    numbers.push(cur);
                }
                if (pre == '-') {
                    numbers.push(-cur);
                }
            }
        }
        int res = 0;
        while (!numbers.isEmpty()) {
            res += numbers.pop();
        }
        return res;
    }

    public static void main(String[] args) {
        L227 test = new L227();
        System.out.println(test.calculate("1 "));
    }

}
